/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hamming;

import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Charles Salles && João Paulo Colling
 */
public class Inicio extends javax.swing.JFrame {

    /**
     * Creates new form Inicio
     */
    private ArrayList<Integer> codigo1;
    private ArrayList<Integer> codigo2;
    private Integer c1;
    private Integer c2;
    private Integer c4;
    private Integer c8;
    private Integer d1;
    private Integer d2;
    private Integer d4;
    private Integer d8;
    private Integer g1;
    private Integer g2;

    public Inicio() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.codigo1 = new ArrayList<>();
        this.codigo2 = new ArrayList<>();

        for (int i = 0; i < this.jTable1.getColumnCount(); i++) {
            if (i != 0 && i != 5 && i != 9 && i != 11 && i != 12) {
                this.jTable1.setValueAt(1, 0, i);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "G", "M8", "M7", "M6", "M5", "C8", "M4", "M3", "M2", "C4", "M1", "C2", "C1"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable2.setRowSelectionAllowed(false);
        jScrollPane2.setViewportView(jTable2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Palavra Transmitida");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Palavra Alterada");

        jButton1.setText("Gerar Código");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Verificar Palavra");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "G", "M8", "M7", "M6", "M5", "C8", "M4", "M3", "M2", "C4", "M1", "C2", "C1"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                true, true, true, true, true, false, true, true, true, false, true, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setRowSelectionAllowed(false);
        jScrollPane3.setViewportView(jTable1);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Palavra Corrigida");

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "M8", "M7", "M6", "M5", "M4", "M3", "M2", "M1"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable3.setRowSelectionAllowed(false);
        jScrollPane4.setViewportView(jTable3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(346, 346, 346)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(351, 351, 351)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 837, Short.MAX_VALUE)
                        .addContainerGap())
                    .addComponent(jScrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(341, 341, 341)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(100, 100, 100)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 657, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jLabel2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(51, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2, jLabel2});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Integer m1;
        Integer m2;
        Integer m3;
        Integer m4;
        Integer m5;
        Integer m6;
        Integer m7;
        Integer m8;

        limpaTabela2();

        if (this.jTable1.getValueAt(0, 1) != null
                && this.jTable1.getValueAt(0, 2) != null
                && this.jTable1.getValueAt(0, 3) != null
                && this.jTable1.getValueAt(0, 4) != null
                && this.jTable1.getValueAt(0, 6) != null
                && this.jTable1.getValueAt(0, 7) != null
                && this.jTable1.getValueAt(0, 8) != null
                && this.jTable1.getValueAt(0, 10) != null) {


            this.codigo1.clear();

            for (int i = 0; i < this.jTable1.getColumnCount(); i++) {
                if (i != 0 && i != 5 && i != 9 && i != 11 && i != 12) {
                    this.codigo1.add((Integer) this.jTable1.getValueAt(0, i));
                    this.jTable2.setValueAt(this.jTable1.getValueAt(0, i), 0, i);
                }
            }

            m8 = this.codigo1.get(0);
            m7 = this.codigo1.get(1);
            m6 = this.codigo1.get(2);
            m5 = this.codigo1.get(3);
            m4 = this.codigo1.get(4);
            m3 = this.codigo1.get(5);
            m2 = this.codigo1.get(6);
            m1 = this.codigo1.get(7);

            c8 = m5 ^ m6 ^ m7 ^ m8;
            c4 = m2 ^ m3 ^ m4 ^ m8;
            c2 = m1 ^ m3 ^ m4 ^ m6 ^ m7;
            c1 = m1 ^ m2 ^ m4 ^ m5 ^ m7;

            g1 = m1 ^ m2 ^ m3 ^ m4 ^ m5 ^ m6 ^ m7 ^ m8 ^ c8 ^ c4 ^ c2 ^ c1;

            this.jTable1.setValueAt(g1, 0, 0);
            this.jTable1.setValueAt(c8, 0, 5);
            this.jTable1.setValueAt(c4, 0, 9);
            this.jTable1.setValueAt(c2, 0, 11);
            this.jTable1.setValueAt(c1, 0, 12);

            this.jTable2.setValueAt(c8, 0, 5);
            this.jTable2.setValueAt(c4, 0, 9);
            this.jTable2.setValueAt(c2, 0, 11);
            this.jTable2.setValueAt(c1, 0, 12);
        } else {
            JOptionPane.showMessageDialog(this, "Preencha todos os campos de Dados.", "Erro", 0);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        if (jTable2.getValueAt(0, 1) != null) {
            Integer m1;
            Integer m2;
            Integer m3;
            Integer m4;
            Integer m5;
            Integer m6;
            Integer m7;
            Integer m8;

            Integer a8;
            Integer a4;
            Integer a2;
            Integer a1;
            this.codigo2.clear();

            limpaTabela3();

            for (int i = 0; i < this.jTable2.getColumnCount(); i++) {
                if (i != 0 && i != 5 && i != 9 && i != 11 && i != 12) {
                    this.codigo2.add((Integer) this.jTable2.getValueAt(0, i));
                }
            }

            m8 = this.codigo2.get(0);
            m7 = this.codigo2.get(1);
            m6 = this.codigo2.get(2);
            m5 = this.codigo2.get(3);
            m4 = this.codigo2.get(4);
            m3 = this.codigo2.get(5);
            m2 = this.codigo2.get(6);
            m1 = this.codigo2.get(7);

            d8 = (Integer) this.jTable2.getValueAt(0, 5);
            d4 = (Integer) this.jTable2.getValueAt(0, 9);
            d2 = (Integer) this.jTable2.getValueAt(0, 11);
            d1 = (Integer) this.jTable2.getValueAt(0, 12);

            g2 = m1 ^ m2 ^ m3 ^ m4 ^ m5 ^ m6 ^ m7 ^ m8 ^ d8 ^ d4 ^ d2 ^ d1;
            
            if ((c8 ^ d8) == 0 && (c4 ^ d4) == 0 && (c2 ^ d2) == 0 && (c1 ^ d1) == 0) {
                d8 = m5 ^ m6 ^ m7 ^ m8;
                d4 = m2 ^ m3 ^ m4 ^ m8;
                d2 = m1 ^ m3 ^ m4 ^ m6 ^ m7;
                d1 = m1 ^ m2 ^ m4 ^ m5 ^ m7;


                this.jTable2.setValueAt(g2, 0, 0);
            }
            
//            this.jTable2.setValueAt(d8, 0, 5);
//            this.jTable2.setValueAt(d4, 0, 9);
//            this.jTable2.setValueAt(d2, 0, 11);
//            this.jTable2.setValueAt(d1, 0, 12);

//            d8 = (Integer) this.jTable2.getValueAt(0, 5);
//            d4 = (Integer) this.jTable2.getValueAt(0, 9);
//            d2 = (Integer) this.jTable2.getValueAt(0, 11);
//            d1 = (Integer) this.jTable2.getValueAt(0, 12);

            String resposta = "";
            if (g1 != g2) {
                String c = ((c8 ^ d8) + "" + (c4 ^ d4) + "" + (c2 ^ d2) + "" + (c1 ^ d1));
                System.out.println(c);
                int pos = Integer.parseInt(c, 2);
                System.out.println(pos);
//                resposta = "";
                switch (pos) {
                    case 1:
                        resposta += "Palavra correta. Erro na transmissão do bit " + c + "\nBit C1 é inválido.";
                        pos = 12;
                        break;
                    case 2:
                        resposta += "Palavra correta. Erro na transmissão do bit " + c + "\nBit C2 é inválido.";
                        pos = 12;
                        break;
                    case 4:
                        resposta += "Palavra correta. Erro na transmissão do bit " + c + "\nBit C4 é inválido.";
                        pos = 12;
                        break;
                    case 8:
                        resposta += "Palavra correta. Erro na transmissão do bit " + c + "\nBit C8 é inválido.";
                        pos = 12;
                        break;
                    case 3:
                        resposta += "Erro na Trasmissão da Palavra no bit " + c + ".\nBit M1 é inválido.";
                        pos = 7;
                        break;
                    case 5:
                        resposta += "Erro na Trasmissão da Palavra no bit " + c + ".\nBit M2 é inválido.";
                        pos = 6;
                        break;
                    case 6:
                        resposta += "Erro na Trasmissão da Palavra no bit " + c + ".\nBit M3 é inválido.";
                        pos = 5;
                        break;
                    case 7:
                        resposta += "Erro na Trasmissão da Palavra no bit " + c + ".\nBit M4 é inválido.";
                        pos = 4;
                        break;
                    case 9:
                        resposta += "Erro na Trasmissão da Palavra no bit " + c + ".\nBit M5 é inválido.";
                        pos = 3;
                        break;
                    case 10:
                        resposta += "Erro na Trasmissão da Palavra no bit " + c + ".\nBit M6 é inválido.";
                        pos = 2;
                        break;
                    case 11:
                        resposta += "Erro na Trasmissão da Palavra no bit " + c + ".\nBit M7 é inválido.";
                        pos = 1;
                        break;
                    case 12:
                        resposta += "Erro na Trasmissão da Palavra no bit " + c + ".\nBit M8 é inválido.";
                        pos = 0;
                        break;
                }
                JOptionPane.showMessageDialog(this, resposta, "Palavra Incorreta", 1);
                int j = 0;
                for (int i = 0; i < this.jTable2.getColumnCount(); i++) {
                    if (i != 0 && i != 5 && i != 9 && i != 11 && i != 12 && pos != 12) {
                        if (pos == j) {
                            System.out.println(this.jTable2.getValueAt(0, i));
                            if (this.jTable2.getValueAt(0, i).equals(0)) {
                                this.jTable3.setValueAt(1, 0, j++);
                            } else {
                                this.jTable3.setValueAt(0, 0, j++);
                            }
                        } else {
                            this.jTable3.setValueAt(this.jTable2.getValueAt(0, i), 0, j++);
                        }
                    }
                }
            } else {
                if ((c8 ^ d8) != 0 || (c4 ^ d4) != 0 || (c2 ^ d2) != 0 || (c1 ^ d1) != 0) {
                    JOptionPane.showMessageDialog(this, "Há mais de um Erro na Transmissão da Palavra.\nA Palavra não Pode Ser Corrigida", "Palavra Incorreta", 1);
                } else {
                    JOptionPane.showMessageDialog(this, "Não há Erro na Transmissão da Palavra.", "Palavra Correta", 1);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Você Deve Gerar o Código da Palavra 1 Antes de Verificar a Palavra 2", "Erro", 0);
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;


                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Inicio.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Inicio.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Inicio.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Inicio.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Inicio().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    // End of variables declaration//GEN-END:variables

    private void limpaTabela3() {
        for (int i = 0; i < jTable3.getColumnCount(); i++) {
            this.jTable3.setValueAt(null, 0, i);
        }
    }

    private void limpaTabela2() {
        for (int i = 0; i < jTable2.getColumnCount(); i++) {
            this.jTable2.setValueAt(null, 0, i);
        }
        limpaTabela3();
    }
}
